package com.siyuan.blog.service.system.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.siyuan.blog.common.base.impl.SuperServiceImpl;
import com.siyuan.blog.common.vo.Menu;
import com.siyuan.blog.dao.system.ResourcesMapper;
import com.siyuan.blog.domain.system.Resources;
import com.siyuan.blog.service.system.LoginService;
import com.siyuan.blog.dao.system.UserMapper;
import com.siyuan.blog.domain.system.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tianxuan
 * @version V1.0
 * @Date 2017/10/16 0016 上午 10:48
 * @Description: 登陆服务
 */
@Service
public class LoginServiceImpl extends SuperServiceImpl<UserMapper,User> implements LoginService {

    @Autowired
    private ResourcesMapper resourcesMapper;
    /**
     * 根据用户名和密码 查询
     * @param userName
     * @param password
     * @return
     */
    public User login (String userName,String password)   {
        User user = super.selectOne(new EntityWrapper<User>().eq("user_name",userName).eq("password",password).eq("status",User.STATUS_ACTIVE));
        return user;
    }

    @Override
    public List<Menu> menus(Integer roleId) {
        List<Menu> menuList = new ArrayList<>();   //  菜单树集合
        Map<String,Menu> menuMap = new HashMap<>();  // map

        List<Resources> modules = resourcesMapper.findListByRole(roleId,Resources.TYPE_MODULE);   //查询出当前角色的所有模块
        List<Resources> pages = resourcesMapper.findListByRole(roleId,Resources.TYPE_PAGE);   //查询出当前角色的所有功能页面

        //将模块放入到菜单树
        for(Resources r : modules) {
            Menu menu = new Menu(r.getCode(), r.getName(), r.getUrl());  // 把模块权限对象 转化为模块菜单对象
            menuList.add(menu);    //  把模块菜单对象 加入到菜单树集合
            menuMap.put(r.getCode(),menu);
        }

        //将页面放入到菜单树
        for(Resources r : pages) {
            Menu menu = new Menu(r.getCode(), r.getName(), r.getUrl().split(",")[0]);   // 把页面权限对象 转化为页面菜单对象
            Menu parentMenu = menuMap.get(r.getParentModule());
            if(null != parentMenu) {
                parentMenu.getSubmenus().add(menu);
            }
        }
        return menuList;
    }

    @Override
    public List<Resources> topMenus(Integer roleId) {
        List<Resources> menuList = resourcesMapper.findListByRoleAndTopNav(roleId, Resources.TYPE_PAGE,1);
        return menuList;
    }

    @Override
    public Map<String, Resources> myResources(Integer roleId) {
        Map<String, Resources> resourcesMap = new HashMap<>();
        List<Resources> allResources = resourcesMapper.findAllListByRole(roleId);
        for(Resources p : allResources) {
            String url = p.getUrl();
            if(!StringUtils.isEmpty(url)){
                String[] urls = url.split(",");
                for(String u : urls){
                    resourcesMap.put(u,p);
                }
            }
        }
        return resourcesMap;
    }

    @Override
    public Map<String, List<String>> myPageResources(Integer roleId) {
        Map<String, List<String>> pageResources = new HashMap<>();
        List<Resources> allResources = resourcesMapper.findAllListByRole(roleId);
        for(Resources p : allResources) {
            int type = p.getType();
            if(type == Resources.TYPE_SUB_PAGE || type == Resources.TYPE_OPT){
                String parentPageCode = p.getParentPage();
                if(pageResources.containsKey(parentPageCode)){
                    pageResources.get(parentPageCode).add(p.getCode());
                }else{
                    List<String> list = new ArrayList();
                    list.add(p.getCode());
                    pageResources.put(parentPageCode,list);
                }
            }
        }
        return pageResources;
    }
}
